Imports System.Text

<System.Runtime.InteropServices.ComVisible(False)> Friend Class ErrorHandler

    'Object used to handle all errors produced by the application.  Writes to a a custom created
    'log source in the Windows event log, and optionally displays the error to the user.

    Dim myLog As EventLog

    Sub New()
        myLog = New EventLog("EQP Bar Code Printing Log")
        myLog.Source = "SBOBarCodePrinting"
    End Sub

    Sub RecordInformation(ByVal Msg As String)
        myLog.WriteEntry(Msg, EventLogEntryType.Information)
    End Sub

    Sub RecordError(ByVal ex As Exception, Optional ByVal ShowToUser As Boolean = False)

        Dim SB As New StringBuilder
        SB.Append("Source:  " & ex.Source & vbCrLf)
        SB.Append("Message:  " & ex.Message & vbCrLf & vbCrLf)
        SB.Append("Stack Trace:  " & ex.StackTrace)

        myLog.WriteEntry(SB.ToString, EventLogEntryType.Error)

        'Optionally show the error to the user
        If ShowToUser = True Then
            MsgBox(SB.ToString, MsgBoxStyle.Critical)
        End If

    End Sub

End Class
